package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import objects.Order_SYD_Child;
import objects.Order_SYD_Parent;

/**
 * Created by Monkey on 2017/3/27.
 * 输液单适配器
 */

public class SYDListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Map<Order_SYD_Parent, List<Order_SYD_Child>> dataSets;
    private List<Order_SYD_Parent> parentsList;
    private List<Boolean> isChoiced;

    public SYDListAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setDataSets(Map<Order_SYD_Parent, List<Order_SYD_Child>> dataSets) {
        this.dataSets = dataSets;
        isChoiced = new ArrayList<>();
        getParentList();
        Collections.sort(parentsList, new Comparator<Order_SYD_Parent>() {
            public int compare(Order_SYD_Parent arg0, Order_SYD_Parent arg1) {
                return arg0.getTMBH().compareTo(arg1.getTMBH());
            }
        });
    }

    private void getParentList() {
        Set set = dataSets.keySet();
        parentsList = new ArrayList<>();
        for (Object aSet : set) {
            parentsList.add((Order_SYD_Parent) aSet);
            isChoiced.add(false);
        }
    }

    public int getChoicedCount() {
        int count = 0;
        if (isChoiced != null) {
            for (boolean choiced : isChoiced) {
                if (choiced) {
                    count++;
                }
            }
        }
        return count;
    }

    public boolean getChoicedItem(int pos) {
        return isChoiced.get(pos);
    }

    public void setChoicedItem(int pos, boolean choiced) {
        isChoiced.set(pos, choiced);
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int i) {
        if (parentsList != null && parentsList.get(i) != null && dataSets.get(parentsList.get(i)) != null) {
            return dataSets.get(parentsList.get(i)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        if (parentsList != null) {
            return parentsList.get(i);
        }
        return null;
    }

    @Override
    public Object getChild(int parentPos, int childPos) {
        if (parentsList != null && dataSets.get(parentsList.get(parentPos)) != null) {
            return dataSets.get(parentsList.get(parentPos)).get(childPos);
        }
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int parentPos, boolean b, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_syd_parent, parent, false);
            holder.sy_cb = (CheckBox) convertView.findViewById(R.id.sy_cb);
            holder.sy_tmbh = (TextView) convertView.findViewById(R.id.tmbh_text);
            holder.sy_ypyf = (TextView) convertView.findViewById(R.id.ypyf_text);
            holder.sy_sypc = (TextView) convertView.findViewById(R.id.sypc_text);
            holder.sy_sysj = (TextView) convertView.findViewById(R.id.sysj_text);
            holder.hdLinear = (LinearLayout) convertView.findViewById(R.id.hd_linear);
            holder.sy_hdzt = (TextView) convertView.findViewById(R.id.hdzt_text);
            holder.sy_hdsj = (TextView) convertView.findViewById(R.id.hdsj_text);
            holder.sy_hdry = (TextView) convertView.findViewById(R.id.hdry_text);
            holder.zxLinear = (LinearLayout) convertView.findViewById(R.id.zx_linear);
            holder.sy_zxsj = (TextView) convertView.findViewById(R.id.zxsj_text);
            holder.sy_zxry = (TextView) convertView.findViewById(R.id.zxry_text);
            holder.sy_zxzt = (TextView) convertView.findViewById(R.id.zxzt_text);
            holder.jsLinear = (LinearLayout) convertView.findViewById(R.id.js_linear);
            holder.sy_jssj = (TextView) convertView.findViewById(R.id.jssj_text);
            holder.sy_jsry = (TextView) convertView.findViewById(R.id.jsry_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        holder.sy_cb.setChecked(isChoiced.get(parentPos));
        holder.sy_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isChoiced.set(parentPos, isChecked);
            }
        });
        if (parentsList != null && parentsList.size() > 0) {
            Order_SYD_Parent orderSydParent = parentsList.get(parentPos);
            if (orderSydParent.getTMBH() != null) {
                holder.sy_tmbh.setText(orderSydParent.getTMBH());
            }
            if (orderSydParent.getYFMC() != null) {
                holder.sy_ypyf.setText(orderSydParent.getYFMC());
            }
            if (orderSydParent.getSYPC() != null) {
                holder.sy_sypc.setText(orderSydParent.getSYPC());
            }
            if (orderSydParent.getSYSJ() != null) {
                holder.sy_sysj.setText(orderSydParent.getSYSJ().substring(5, 16));
            }
            if (orderSydParent.getHDBZ() != null && orderSydParent.getHDBZ().equals("1")) {
                holder.sy_hdzt.setText("已核对");
                holder.sy_hdzt.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
                holder.hdLinear.setVisibility(View.VISIBLE);
                if (orderSydParent.getHDSJ() != null) {
                    String text = mContext.getString(R.string.syd_text_1) + orderSydParent.getHDSJ().substring(0, 19);
                    holder.sy_hdsj.setText(text);
                }
                if (orderSydParent.getYHXM() != null) {
                    String text = mContext.getString(R.string.syd_text_2) + orderSydParent.getYHXM();
                    holder.sy_hdry.setText(text);
                }
                holder.sy_zxzt.setVisibility(View.VISIBLE);
                if (orderSydParent.getSYZT() != null) {//0.未执行 1.已执行  2.执行中  4.暂停中
                    String text = "";
                    switch (orderSydParent.getSYZT()) {
                        case "0":
                            text = "未执行";
                            holder.sy_zxzt.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                            break;
                        case "1":
                            text = "已执行";
                            holder.sy_zxzt.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
                            holder.jsLinear.setVisibility(View.VISIBLE);
                            String jssj = orderSydParent.getJSSJ() == null ? "" : orderSydParent.getJSSJ().substring(0, 19);
                            jssj = mContext.getString(R.string.syd_text_10) + jssj;
                            holder.sy_jssj.setText(jssj);
                            String jsyg = orderSydParent.getJSYG() == null ? "" : orderSydParent.getJSYG();
                            jsyg = mContext.getString(R.string.syd_text_11) + jsyg;
                            holder.sy_jsry.setText(jsyg);
                            break;
                        case "2":
                            text = "执行中";
                            holder.sy_zxzt.setTextColor(mContext.getResources().getColor(R.color.holo_green_dark));
                            break;
                        case "4":
                            text = "暂停中";
                            holder.sy_zxzt.setTextColor(mContext.getResources().getColor(R.color.colorYellow));
                            break;
                        default:
                            break;
                    }
                    holder.sy_zxzt.setText(text);
                    if (!orderSydParent.getSYZT().equals("0")) {
                        holder.zxLinear.setVisibility(View.VISIBLE);
                    }
                    String zxsj = orderSydParent.getKSSJ() == null ? "" : orderSydParent.getKSSJ().substring(0, 19);
                    zxsj = mContext.getString(R.string.syd_text_8) + zxsj;
                    holder.sy_zxsj.setText(zxsj);
                    String zxry = orderSydParent.getKSYG() == null ? "" : orderSydParent.getKSYG();
                    zxry = mContext.getString(R.string.syd_text_9) + zxry;
                    holder.sy_zxry.setText(zxry);
                }

            } else {
                holder.sy_hdzt.setText("未核对");
                holder.sy_hdzt.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                holder.hdLinear.setVisibility(View.GONE);
                holder.sy_zxzt.setVisibility(View.GONE);
                holder.zxLinear.setVisibility(View.GONE);
                holder.jsLinear.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int parentPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        List<Order_SYD_Child> list = dataSets.get(parentsList.get(parentPos));
        Collections.sort(list, new Comparator<Order_SYD_Child>() {
            public int compare(Order_SYD_Child arg0, Order_SYD_Child arg1) {
                return arg0.getYZXH().compareTo(arg1.getYZXH());
            }
        });
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_syd_child, parent, false);
            holder.sy_yzxh = (TextView) convertView.findViewById(R.id.yzxh_text);
            holder.sy_ycjl = (TextView) convertView.findViewById(R.id.ycjl_text);
            holder.sy_jldw = (TextView) convertView.findViewById(R.id.jldw_text);
            holder.sy_ycsl = (TextView) convertView.findViewById(R.id.ycsl_text);
            holder.sy_sldw = (TextView) convertView.findViewById(R.id.sldw_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            Order_SYD_Child orderSydChild = list.get(childPos);
            if (orderSydChild.getYZMC() != null) {
                holder.sy_yzxh.setText(orderSydChild.getYZMC());
            }
            if (orderSydChild.getYCJL() != null) {
                holder.sy_ycjl.setText(orderSydChild.getYCJL());
            }
            if (orderSydChild.getJLDW() != null) {
                holder.sy_jldw.setText(orderSydChild.getJLDW());
            }
            if (orderSydChild.getYCSL() != null) {
                holder.sy_ycsl.setText(orderSydChild.getYCSL());
            }
            if (orderSydChild.getSLDW() != null) {
                holder.sy_sldw.setText(orderSydChild.getSLDW());
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    private class ParentViewHolder {
        CheckBox sy_cb;
        TextView sy_tmbh;
        TextView sy_ypyf;
        TextView sy_sypc;
        TextView sy_hdzt;
        TextView sy_zxzt;
        TextView sy_sysj;
        LinearLayout hdLinear;
        TextView sy_hdsj;
        TextView sy_hdry;
        LinearLayout zxLinear;
        TextView sy_zxsj;
        TextView sy_zxry;
        LinearLayout jsLinear;
        TextView sy_jssj;
        TextView sy_jsry;
    }

    private class ChildViewHolder {
        TextView sy_yzxh;
        TextView sy_ycjl;
        TextView sy_jldw;
        TextView sy_ycsl;
        TextView sy_sldw;
    }

    public void updateBackGroup(int position, boolean choiced) {
        isChoiced.set(position, choiced);
    }

}
